//
//  CategoryViewController.m
//  ZCooking
//
//  Created by 韩旭 on 16/8/27.
//  Copyright © 2016年 HX_. All rights reserved.
//

#import "CategoryViewController.h"
#import "HX_Macro.h"
#import "HXFoodCollectionCell.h"
#import "HXFoodCustomCell.h"
#import "HXCategoryBigModel.h"
#import "HXCategroyTCell.h"
#import "HXCategorySmallModel.h"
#import "HXCategoryDetailVC.h"
#define kTableView @"tableView"
@interface CategoryViewController ()<UITableViewDataSource, UITableViewDelegate>
/**
 *  模型外层数组
 */
@property (nonatomic, strong) NSMutableArray *bigArr;
/**
 *  里层模型数组
 */
@property (nonatomic, strong) NSMutableArray *smallArr;

@property (nonatomic, strong) UITableView *tableView;
@end

@implementation CategoryViewController
- (NSMutableArray *)bigArr {
    if (!_bigArr) {
        _bigArr = [NSMutableArray array];
    }
    return _bigArr;
}
- (NSMutableArray *)smallArr {
    if (!_smallArr) {
        _smallArr = [NSMutableArray array];
    }
    return _smallArr;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self getNetWorking];
    [self creatTableView];
}
- (void)getNetWorking {
    NSString *url = @"http://api.izhangchu.com/";
    NSString *body = @"methodName=CategoryIndex&version=4.40&";
    NSDictionary *dicHeader = @{@"Content-Type" : @"application/x-www-form-urlencoded; charset=UTF-8", @"User-Agent" : @"Dalvik/1.6.0 (Linux; U; Android 4.3; R6007 Build/JLS36C)zhangchuAPPforAndroid4.4.0", @"Host" : @"api.izhangchu.com", @"Connection" : @"Keep-Alive", @"Accept-Encoding" : @"gzip"};
    [HXNetTool postNetWithUrl:url body:body BodyStyle:HXBodyString Header:dicHeader Response:HXJSON Success:^(id result) {
        NSDictionary *results = result;
        NSDictionary *dicData = results[@"data"];
        NSArray *arrData = dicData[@"data"];
        for (NSDictionary *dic in arrData) {
         HXCategoryBigModel *bigModel =     [HXCategoryBigModel ModelWithDic:dic];
            [self.bigArr addObject:bigModel];
            [bigModel.data insertObject:bigModel.image atIndex:0];
        }
        [_tableView reloadData];
    } Failure:^(NSError *error) {
    }];
}
- (void)creatTableView {
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 110) style:0];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_tableView];
    [_tableView registerClass:[HXCategroyTCell class] forCellReuseIdentifier:kTableView];
    _tableView.separatorStyle = NO;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.bigArr.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HXCategroyTCell *cell = [tableView dequeueReusableCellWithIdentifier:kTableView];
    HXCategoryBigModel *bigModel = self.bigArr[indexPath.section];
    cell.categroy = bigModel.data;
    cell.items = ^(NSInteger rows) {
        HXCategoryDetailVC *detail = [[HXCategoryDetailVC alloc] init];
        detail.netId = [bigModel.data[rows] ID];
        detail.title = [bigModel.data[rows] text];
        [self.navigationController pushViewController:detail animated:YES];
    };
    return cell;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *view = [[UIView alloc] init];
    view.backgroundColor = [UIColor whiteColor];
    UILabel *lable = [[UILabel alloc] init];
    HXCategoryBigModel *model = self.bigArr[section];
    lable.text = model.text;
    lable.textColor = [UIColor colorWithWhite:0.062 alpha:1.000];
    lable.font = [UIFont systemFontOfSize:15];
    lable.HX_x = 20;
    lable.HX_height = 49;
    lable.HX_width = [SelfSizeTools selfSizeHeightWithString:model.text height:49 Font:[UIFont systemFontOfSize:17]];
    [view addSubview:lable];
    return view;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 49;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    HXCategoryBigModel *bigModel = self.bigArr[indexPath.section];
     NSInteger cellCount = (bigModel.data.count - 7);
    if (bigModel.data.count > 7) {
        if (cellCount % 5 == 0) {
            return cellCount / 5 * 40 + cellCount * 3 + 40 * 2;
        } else {
            return (cellCount / 5 + 3) * 40 + (cellCount + 5) * 2;
        }
    } else {
        return 40 * 2 + 2 * 3;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
